/*
 * Copyright (c) 2011, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the [QTPRJ] nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL [QTPRJ] BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package joodin.impl.widgets.internal;

import joodin.impl.application.util.ApplicationWrapper;

import org.jowidgets.common.widgets.controler.IActionListener;
import org.jowidgets.spi.impl.controler.ActionObservable;
import org.jowidgets.spi.widgets.IToolBarButtonSpi;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class ToolBarButtonImpl extends ToolBarItemImpl implements IToolBarButtonSpi {

	private final ActionObservable actionObservable;

	@SuppressWarnings("serial")
	public ToolBarButtonImpl(final Button button) {
		super(button);
		this.actionObservable = new ActionObservable();
		
		String toolbar = ".toolbar .v-button {display: block; height: 55px; background: transparent; border: none; text-align: center;}" +
		".toolbar .v-button img { display: block; margin-left: auto; margin-right: auto; margin-bottom: 5px;}" +
		" .toolbar .v-button span { font-size: x-small;text-shadow: #fafafa 1px 1px 0;}" +
		".toolbar .v-button .v-button-wrap, .toolbar .v-disabled.v-button .v-button-wrap { background: transparent;" +
		" border: none; -webkit-border-radius: 0; -moz-border-radius: 0; border-radius: 0; -webkit-box-shadow: none;" +
		" -moz-box-shadow: none; box-shadow: none;} .toolbar .v-button:active .v-button-wrap, .toolbar .v-button.v-pressed " +
		".v-button-wrap { background: transparent; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; \n";
		
		ApplicationWrapper.getCSSInjector().setValue(ApplicationWrapper.getCSSInjector().getValue() + ".foregroundColor-123 .v-nativebutton{ color: rgb(0,70, 213); } .bold-123 .v-nativebutton{ font-weight:bold;}\n" );
		
		button.addStyleName("foregroundColor-123");
		button.addStyleName("bold-123");

		getUiReference().addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				fireActionPerformed();
			}
		});
	}
	
	@Override
	public Button getUiReference() {
		return (Button)super.getUiReference();
	}

	protected void fireActionPerformed() {
		actionObservable.fireActionPerformed();
	}

	@Override
	public void addActionListener(final IActionListener actionListener) {
		actionObservable.addActionListener(actionListener);
	}

	@Override
	public void removeActionListener(final IActionListener actionListener) {
		actionObservable.removeActionListener(actionListener);
	}

}
